package com.usernet.product.utils;

import java.util.UUID;

public class StringUtils {
	
	/**
	 * 获取UUID
	 * 
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}
	/*
	 * 计算字符串的字节长度(字母数字计1，汉字及标点计2)
	 */
	public static int byteLength(String string) {
		int count = 0;
		for (int i = 0; i < string.length(); i++) {
			if (Integer.toHexString(string.charAt(i)).length() == 4) {
				count += 2;
			} else {
				count++;
			}
		}
		return count;
	}

	/**
	 * 按指定长度，省略字符串部分字符
	 * 
	 * @para String 字符串
	 * @para length 保留字符串长度
	 * 
	 * @return 省略后的字符串 　　
	 */
	public static String omitString(String string, int length) {
		StringBuffer sb = new StringBuffer();
		if (byteLength(string) > length) {
			int count = 0;
			for (int i = 0; i < string.length(); i++) {
				char temp = string.charAt(i);
				if (Integer.toHexString(temp).length() == 4) {
					count += 2;
				} else {
					count++;
				}
				if (count < length - 3) {
					sb.append(temp);
				}
				if (count == length - 3) {
					sb.append(temp);
					break;
				}
				if (count > length - 3) {
					sb.append(" ");
					break;
				}
			}
			sb.append("...");
		} else {
			sb.append(string);
		}
		return sb.toString();
	}

	public static String marginString(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append(".");
		}
		return sb.toString();
	}
	public static String avgString1(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append("<span></span>");
		}
		return sb.toString();
	}
	public static String avgString2(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append("<span class=\"gray\"></span>");
		}
		return sb.toString();
	}
}
